﻿using ComTools.Helpers;
using SuperSocket.SocketBase.Command;
using SuperSocketService.AppBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToolsModel;

namespace SuperSocketService.Commands
{
    public class AskAnswer : CommandBase<MySession, MyRequestInfo>
    {
        private int Action = 10;
        public override string Name
        {
            get { return Action.ToString(); }
        }

        /// <summary>
        /// 上行（来自客户端的信息）
        /// </summary>
        /// <param name="session"></param>
        /// <param name="requestInfo"></param>
        public override void ExecuteCommand(MySession session, MyRequestInfo requestInfo)
        {
            //添加判断登陆是否可以成功的逻辑
            var body = requestInfo.Body;
            session.OrgCode = body;
            //LogHelper.WriteLog("命令被执行");
            //LogHelper.WriteLog("用户是：" + requestInfo.Body);
            //LogHelper.WriteLog("用户ID是：" + session.SessionID);
            Dictionary<string, object> requestStr = body.JSONToObject<Dictionary<string, object>>();
            var result = new ResultJsonInfo<string>();
            result.Data = "Action=10";
            Push(session, 1, result.ObjectToJSON());
        }

        /// <summary>
        ///  下行(推送)
        /// </summary>
        public void Push(MySession session, byte status, string resultInfo)
        {
            var response = BitConverter.GetBytes((ushort)resultInfo.Length).Reverse().ToList();
            var arr = Encoding.UTF8.GetBytes(resultInfo);
            response.AddRange(BitConverter.GetBytes((ushort)arr.Length).Reverse().ToArray());
            response.AddRange(arr);
            session.Send(response.ToArray(), 0, response.Count);
        }
    }
}
